package com.chery.foxlogrecord.extension.operation.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chery.foxlogrecord.extension.operation.entity.OperationField;
import com.chery.foxlogrecord.extension.operation.mapper.OperationFieldMapper;
import com.chery.foxlogrecord.extension.operation.pojo.dto.OperationFieldPageDto;
import com.chery.foxlogrecord.extension.operation.pojo.vo.OperationFieldVo;
import com.chery.foxlogrecord.extension.operation.service.OperationFieldService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;

/**
 * @author qiangt
 * @version 1.0
 * @date 2023/12/5 11:11
 * @description: 操作字段service
 */
@RequiredArgsConstructor
@Validated
@Service
public class OperationFieldServiceImpl extends ServiceImpl<OperationFieldMapper, OperationField>
        implements OperationFieldService {

    private final OperationFieldMapper operationFieldMapper;

    @Override
    public Page<OperationField> pageByOperationId(@Valid OperationFieldPageDto pageDto) {
        Page<OperationField> page = new Page<>(pageDto.getPageCurrent(), pageDto.getPageSize());
        String orderSql = pageDto.getOrderSql();
        return this.lambdaQuery()
                .eq(OperationField::getOperationId, pageDto.getOperationId())
                .last(StrUtil.isNotBlank(orderSql), orderSql)
                .orderByDesc(StrUtil.isBlank(orderSql), OperationField::getCreateTime)
                .page(page);
    }

    @Override
    public Page<OperationFieldVo> pageWithJoin(OperationFieldPageDto pageDto) {
        Page<OperationFieldVo> page = new Page<>(pageDto.getPageCurrent(), pageDto.getPageSize());
        List<OperationFieldVo> operationFieldVos = operationFieldMapper.pageWithJoin(pageDto, page);
        return page.setRecords(operationFieldVos);
    }
}
